/*
 * AS3 MultiTouch Client
 *
 * Cursor - A "finger" on the touchable area.  
 *
 * Copyright 2008 Artifacts - Fine Software Development
 * http://www.artifacts.de
 * Author: Michael Markowski (m.markowski@artifacts.de)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

/**
 * Cursor - A "finger" on the touchable area.
 * A Cursor represents a finger. It has a width, height and position.
 * An touchable object is hit by a cursor, when the bounding box of the touchable
 * intersects with the bounding box of the cursor.
 */
 
package de.artifacts.multitouch
{
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class Cursor extends Sprite
	{
		public var position : Point;
		public var id : int;
		public var cursorWidth : Number;
		public var cursorHeight : Number;
		private var outline : Sprite;
		public var touchables : Array;
		public var tapCount : int;
		public var shouldSurpressInterpretationAsTap : Boolean = false;
		
		public function Cursor(x : Number, y : Number, w : Number, h : Number, id : int)
		{
			this.position = new Point(x, y);
			this.cursorWidth = w;
			this.cursorHeight = h; 
			this.id = id;
			this.outline = new Sprite();
			this.addChild(outline);
			this.touchables = new Array();
		}
		
		public function hits(touchable : Touchable) : Boolean
		{		
			var cursorRect : Rectangle = new Rectangle(this.position.x, this.position.y, this.cursorWidth, this.cursorHeight);
			var touchableRect : Rectangle = touchable.wrappee.getBounds(touchable.touchManager.touchArea);
			var hits : Boolean = touchableRect.intersects(cursorRect);
			return hits;
		}		
		
		public function drawFingerprint() : void
		{
			this.outline.graphics.clear();
			this.outline.graphics.lineStyle(1, 0x0000ff);			
			this.outline.graphics.drawRect(position.x, position.y, cursorWidth, cursorHeight);
		}
		
		public function touches(touchable : Touchable) : Boolean
		{
			touchables.sortOn("zIndex", Array.NUMERIC | Array.DESCENDING);									
			return (touchables[0] == touchable);
		}
		
		override public function toString() : String
		{
			return "[cursor: id="+id+": " + this.position.x + ", " + this.position.y + " (" + this.cursorWidth + ", " + this.cursorHeight +")]";
		}				
	}
}